package com.opennetexam.manager.controller;

import com.opennetexam.manager.bussinessservice.ExamPhotoService;
import com.opennetexam.manager.bussinessservice.ExamPlanInfoService;
import com.opennetexam.manager.common.CommonFunction;
import com.opennetexam.manager.common.OperationResult;
import com.opennetexam.manager.dto.AdminInfo;
import com.opennetexam.manager.dto.ExamAdminInfoDTO;
import com.opennetexam.manager.dto.ExaminerPhotoDTO;
import com.opennetexam.manager.enums.OperationResultType;
import com.opennetexam.manager.socket.WebSocketServer;
import com.opennetexam.manager.utils.CommonUtil;
import com.opennetexam.manager.utils.FileUtil;
import com.opennetexam.manager.utils.GsonUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Created by admin on 2017/11/22.
 * 照片采集
 */
@Controller
@RequestMapping("/Photo")
public class PhotoController extends BaseController{

    private static final Logger logger = Logger.getLogger(PhotoController.class);

    @Autowired
    private ExamPhotoService examPhotoService;
    @Autowired
    private ExamPlanInfoService examPlanInfoService;


    /**
     * 获取考生照片列表
     * @param requestBody
     * @param request
     * @param response
     */
    @RequestMapping(value = "/GetList",method = RequestMethod.POST)
    public void getPhotoList(@RequestBody Map<String,Object> requestBody, HttpServletRequest request, HttpServletResponse response){
        OperationResult operationResult;
        try {
            String examineeName = "";//学生姓名
            String examCardNo = "";//准考证号
            //当前用户信息
            String account = (String) requestBody.get("account");
            ExamAdminInfoDTO adminUserInfo = getAdminUserInfo(account);
            String planId = (String) requestBody.get("planId");
            String roomId = (String) requestBody.get("roomId");
            String sceneId = (String) requestBody.get("sceneId");
            Integer statusId = (Integer) requestBody.get("statusId");
            String parameter = (String) requestBody.get("parameter");

            // 如果是汉字则为名称检索，否则为准考证号检索
            if(!StringUtils.isEmpty(parameter)){
            String reg = "^[\\u4e00-\\u9fa5]+$";
            if(CommonUtil.matchesStr(parameter,reg)){
                examineeName = parameter.trim();
            }else {
                examCardNo = parameter.trim();
            }
            }
            operationResult = examPhotoService.getPhotoList(planId,roomId,sceneId,statusId,examineeName,examCardNo, adminUserInfo.getAppID());
        } catch (Exception e) {
            logger.error("GetPhotoListException",e);
            operationResult = new OperationResult(OperationResultType.Error,OperationResultType.Error.getDisplay());
        }
        CommonFunction.responseWrite(response,operationResult);
    }

    /**
     * 获取考生照片
     * @param requestBody
     * @param response
     */
    @RequestMapping(value = "/findPhotoList",method = RequestMethod.POST)
    public void findPhotoList(@RequestBody Map<String,Object> requestBody,HttpServletResponse response){
        //考生编排ID
        String examinerArrangeID = requestBody.get("examineeArrangeID").toString();
        CommonFunction.responseWrite(response,examPhotoService.getExaminerPhotoByType(examinerArrangeID));
    }

    /**
     * 管理端主动拍照
     * @return
     */
    @RequestMapping(value="/pushMessageToClient",method=RequestMethod.POST)
    public void pushVideoListToWeb(@RequestBody Map<String,Object> requestParam,HttpServletResponse response) {
        OperationResult operationResult = null;
        Map<String,Object> responseParam =new HashMap<String,Object>();
        try {
            //发送拍照消息给客户端
            responseParam.put("TakePhoto",true);
            responseParam.put("ExamCardNO",requestParam.get("ExamCardNO"));
            responseParam.put("SubjectID",requestParam.get("SubjectID"));
            WebSocketServer.sendInfo(GsonUtil.toJson(responseParam));
            operationResult = new OperationResult(OperationResultType.Success);
        }catch (IOException e) {
            operationResult = new OperationResult(OperationResultType.Error);
        }
        CommonFunction.responseWrite(response,operationResult);
    }
}
